<?php

namespace Frieza\Modules\PublishSetting\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;
use Doraemon\model\PublishSetting\PublishSetting as mPublishSetting;

class PublisherController extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index(Request $request, Content $content): Content
    {
        $info = $this->_formatPublisher();
        $form = new Form($info);
        $form->html('<h4><b>动态发布器默认文案</b></h4>');
        $form->textarea('note_content_desc_user_1', '普通用户')
            ->placeholder('支持换行')
            ->required(true);
        $form->textarea('note_content_desc_user_2', '设计师/装企/品牌')
            ->placeholder('支持换行')
            ->required(true);
        $form->html('<h4><b>日记发布器默认文案</b></h4>');
        $form->textarea('diary_content_desc', '普通用户')
            ->placeholder('支持换行')
            ->required(true);
        $form->html('<h4><b>动态内容发布提示弹层文案</b></h4>');
        $form->textarea('note_popup_title', '标题文案')
            ->placeholder('支持换行')
            ->required(true);
        $form->textarea('note_popup_content', '描述文案')
            ->placeholder('支持换行')
            ->required(true);
        $form->html('<h4><b>日记内容发布提示弹层文案</b></h4>');
        $form->textarea('diary_popup_title', '标题文案')
            ->placeholder('支持换行')
            ->required(true);
        $form->textarea('diary_popup_content', '描述文案')
            ->placeholder('支持换行')
            ->required(true);
        $form->hidden('note_publisher_id1');
        $form->hidden('note_publisher_id2');
        $form->hidden('diary_publisher_id');

        $form->action(url('admin/publishSetting/publisher/index/save'));
        return $content->header('发布器默认文案')->breadcrumb(
            ['text' => '发布器默认文案'],
        )->row($form->render());
        return $content
            ->header('发布器默认文案')
            ->breadcrumb(['text' => '发布器默认文案']);
    }


    public function save(Request $request)
    {
        try {
            $noteContentDescUserCommon = $request->input('note_content_desc_user_1', '');
            $noteContentDescUserCompany = $request->input('note_content_desc_user_2', '');
            $diaryContentDesc = $request->input('diary_content_desc', '');
            $notePopupTitle = $request->input('note_popup_title', '');
            $notePopupContent = $request->input('note_popup_content', '');
            $diaryPopupTitle = $request->input('diary_popup_title', '');
            $diaryPopupContent = $request->input('diary_popup_content', '');
            $notePublisherId1 = $request->input('note_publisher_id1', '');
            $notePublisherId2 = $request->input('note_publisher_id2', '');
            $diaryPublisherId = $request->input('diary_publisher_id', '');
            if (empty($noteContentDescUserCommon) || empty($noteContentDescUserCompany) || empty($diaryContentDesc)
                || empty($notePopupTitle) || empty($notePopupContent) || empty($diaryPopupTitle) || empty($diaryPopupContent)) {
                return back()->withInput()->with(['error' => new MessageBag([
                    'title' => '参数不能为空',
                ])]);
            }

            //数据都不能为空
            $diaryPublishSettingInfo = mPublishSetting::getById($diaryPublisherId);
            if (!empty($diaryPublisherId) && empty($diaryPublishSettingInfo)) {
                return back()->withInput()->with(['error' => new MessageBag([
                    'title' => 'id错误，数据不存在',
                ])]);
            }
            $time = time();
            $operId = Admin::user()->id;
            //全部没有创建
            if (empty($diaryPublishSettingInfo)) {
                //普通用户的note配置
                mPublishSetting::add([
                    'user_type' => mPublishSetting::USER_TYPE_COMMON,
                    'publish_type' => mPublishSetting::PUBLISH_TYPE_NORMAL_NOTE,
                    'content_desc' => $noteContentDescUserCommon,
                    'popup_title' => $notePopupTitle,
                    'popup_content' => $notePopupContent,
                    'oper_id' => $operId,
                    'addtime' => $time,
                    'edittime' => $time,
                ]);
                //商业用户的note配置
                mPublishSetting::add([
                    'user_type' => mPublishSetting::USER_TYPE_COMPANY,
                    'publish_type' => mPublishSetting::PUBLISH_TYPE_NORMAL_NOTE,
                    'content_desc' => $noteContentDescUserCompany,
                    'popup_title' => $notePopupTitle,
                    'popup_content' => $notePopupContent,
                    'oper_id' => $operId,
                    'addtime' => $time,
                    'edittime' => $time,
                ]);
                //装修日记的配置
                mPublishSetting::add([
                    'publish_type' => mPublishSetting::PUBLISH_TYPE_DIARY,
                    'user_type' => mPublishSetting::USER_TYPE_COMMON,
                    'content_desc' => $diaryContentDesc,
                    'popup_title' => $diaryPopupTitle,
                    'popup_content' => $diaryPopupContent,
                    'oper_id' => $operId,
                    'addtime' => $time,
                    'edittime' => $time,
                ]);
            } else {
                mPublishSetting::updateById($notePublisherId1, [
                    'content_desc' => $noteContentDescUserCommon,
                    'popup_title' => $notePopupTitle,
                    'popup_content' => $notePopupContent,
                    'oper_id' => $operId,
                    'edittime' => $time,
                ]);
                mPublishSetting::updateById($notePublisherId2, [
                    'content_desc' => $noteContentDescUserCompany,
                    'popup_title' => $notePopupTitle,
                    'popup_content' => $notePopupContent,
                    'oper_id' => $operId,
                    'edittime' => $time,
                ]);
                mPublishSetting::updateById($diaryPublisherId, [
                    'content_desc' => $diaryContentDesc,
                    'popup_title' => $diaryPopupTitle,
                    'popup_content' => $diaryPopupContent,
                    'oper_id' => $operId,
                    'edittime' => $time,
                ]);
            }
            $success = new MessageBag([
                'title' => '操作成功'
            ]);
            return redirect(url('admin/publishSetting/publisher/index'))->with(compact('success'));
        } catch (\Exception $e) {
            $error = new MessageBag([
                'title' => '操作失败,信息:' . $e->getMessage()
            ]);
            return back()->withInput()->with(compact('error'));
        }
    }

    private function _formatPublisher()
    {
        $notePublishSettingInfo1 = mPublishSetting::getByPublishTypeAndUserType(mPublishSetting::PUBLISH_TYPE_NORMAL_NOTE, mPublishSetting::USER_TYPE_COMMON);
        $notePublishSettingInfo2 = mPublishSetting::getByPublishTypeAndUserType(mPublishSetting::PUBLISH_TYPE_NORMAL_NOTE, mPublishSetting::USER_TYPE_COMPANY);
        $diaryPublishSettingInfo = mPublishSetting::getByPublishTypeAndUserType(mPublishSetting::PUBLISH_TYPE_DIARY);
        return [
            'note_publisher_id1' => $notePublishSettingInfo1['id'] ?? '',
            'note_publisher_id2' => $notePublishSettingInfo2['id'] ?? '',
            'diary_publisher_id' => $diaryPublishSettingInfo['id'] ?? '',
            'note_content_desc_user_1' => $notePublishSettingInfo1['content_desc'] ?? '',
            'note_content_desc_user_2' => $notePublishSettingInfo2['content_desc'] ?? '',
            'diary_content_desc' => $diaryPublishSettingInfo['content_desc'] ?? '',
            'note_popup_title' => $notePublishSettingInfo1['popup_title'] ?? '',
            'note_popup_content' => $notePublishSettingInfo1['popup_content'] ?? '',
            'diary_popup_title' => $diaryPublishSettingInfo['popup_title'] ?? '',
            'diary_popup_content' => $diaryPublishSettingInfo['popup_content'] ?? '',
        ];
    }
}
